package momo.multitree.util;

import java.io.FileReader;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import momo.multitree.simulation.OptStarTree;
import momo.multitree.structure.Graph;
import momo.multitree.structure.Tree;
import momo.multitree.visual.GraphVisualOutput;

public class StarTreeChecker {
	
	private Log log = LogFactory.getLog(StarTreeChecker.class);
	
	public StarTreeChecker() {
	}

	public void check(String filename) {
		try
		{
			Graph g = new Graph(new FileReader("dataset/"+filename));
//			g.outputVisual("graph_" + filename);
			OptStarTree sim = new OptStarTree(g);
			Set<Tree> starSet = sim.getAllPossibleStarTree();
			Tree bestLatTree = sim.getBestLatTree();
//			bestLatTree.outputVisual("lat_" + filename);
			Tree bestCostTree = sim.getBestCostTree();
//			bestCostTree.outputVisual("cost_" + filename);
			Tree bestStabTree = sim.getBestStabTree();
//			bestStabTree.outputVisual("stab_" + filename);
			for ( Tree star: starSet ) {
//				star.outputVisual("star_" + star.getRoot().getId());
				log.info(filename+", " + this.displayInfo(bestLatTree, bestCostTree, bestStabTree, star));
			}
		}catch(Exception e)
		{
			log.error("Error on filename: " + filename, e);
			e.printStackTrace();
		}
	}
	
	public String displayInfo(Tree bestLatTree, Tree bestCostTree, Tree bestStabTree, Tree bestTree)
	{
		return bestTree.getRoot().getId() + ", " +
				bestLatTree.compWeightedLatency() + ", " +
				bestLatTree.compCost(false) + ", " +
				bestLatTree.compStability() + ", " +
				bestCostTree.compWeightedLatency() + ", " +
				bestCostTree.compCost(false) + ", " +
				bestCostTree.compStability() + ", " +
				bestStabTree.compWeightedLatency() + ", " +
				bestStabTree.compCost(false) + ", " +
				bestStabTree.compStability() + ", " +
				bestTree.compWeightedLatency() + ", " +
				bestTree.compCost(false) + ", " +
				bestTree.compStability();
	}
	
	public static void main(String args[]) {
		String filename = "15_node_symmetric_noleaf_dataset_5.txt";
		StarTreeChecker app = new StarTreeChecker();
		app.check(filename);
	}
	
}//end of class StarTreeChecker
